'use strict';
// 申请开通控制器模块

var wcom = require("../components/winLocation.js");
import {
    viewInit
} from '../views/forgotPasswordView.js';
import {
    errorDialog
} from '../components/errorDialog.js';
import {
    Logout
} from '../components/logout.js';
var logoutObject;

$(document).on("pageinit", "#forgotPassword", controllerInit);

var role;
var $qyName, $licence_no, $frName, $frId, $jsName, $jsNum, $fistSubmit, $lodinPassword, $passwordAgain, $nextSubmit;;
// 控制器初始化
function controllerInit() {
    wcom.checkBrowser();
    initValue();
    viewInit(role);
    bindEvent();
}

// 绑定事件
function bindEvent() {
    $nextSubmit.on("touchstart", changePassword);
    $fistSubmit.on("touchend", function() {
        if ($fistSubmit.hasClass("weui-btn_disabled")) {
            return;
        } else {
            validateInput()

        }
    });
}

// 验证输入
function validateInput() {
    var parameters = getParameters();
    parameters.state = 1;
    $.post(MerReplace_URL, parameters, viback);
}

// 验证完后回调函数
function viback(cbdata) {
    if (cbdata.lfq_merextrainfo_response && cbdata.lfq_merextrainfo_response.resp_code == "0000") {
        $("#merchant-form").hide();
        $("#next-form").show();
    } else if (cbdata.lfq_merextrainfo_response && cbdata.lfq_merextrainfo_response.resp_msg) {
        errorObject.showErroDialog("填写信息有误,请重新填写");
    }
}

// 重置密码
function changePassword() {
    var parameters = getParameters();
    parameters.state = 2;
    $.post(MerReplace_URL, parameters, cbfuc)
}

//修改密码回调函数
function cbfuc(cbdata) {
    console.dir(cbdata);
    if (cbdata.lfq_merextrainfo_response && cbdata.lfq_merextrainfo_response.resp_code == "0000") {
        errorObject.showErroDialog("重置密码成功");
        setTimeout(function() {
            logoutObject._removeSessionStorage();
            logoutObject.setLocalStorageLogout();
            location.href = LoginInit_URL;
        }, 3000);
    } else if (cbdata.lfq_merextrainfo_response && cbdata.lfq_merextrainfo_response.msg) {
        errorObject.showErroDialog(cbdata.lfq_merextrainfo_response.msg);
    } else if(cbdata.lfq_merextrainfo_response && cbdata.lfq_merextrainfo_response.resp_msg) {
        errorObject.showErroDialog(cbdata.lfq_merextrainfo_response.resp_msg);
    }else{
         errorObject.showErroDialog("重置密码失败");
    }

}
// 取参数
function getParameters() {
    var qyNameVal = $qyName.val();
    var licenceNoVal = $licence_no.val();
    var frNameVal = $frName.val();
    var frIdVal = $frId.val();
    var jsNameVal = $jsName.val();
    var jsNumVal = $jsNum.val();
    var passwordVal = $lodinPassword.val();
    var passwordAgainVal = $passwordAgain.val();

    var parameters = {
        corpComRegName: qyNameVal,
        bizLicNo: licenceNoVal,
        corporateName: frNameVal,
        corporateId: frIdVal,
        balanceAcctName: jsNameVal,
        balanceAcctCode: jsNumVal,
        newPassWord: passwordAgainVal,
        nowTime: (new Date()).getTime()
    };

    return parameters;
}

// 初始化变量
function initValue() {
    wcom.QueryString.Initial();
    if (wcom.QueryString.GetValue("role")) {
        role = wcom.QueryString.GetValue("role");
    } else {
        role = "managers";
    }
    $qyName = $("#qyName");
    $licence_no = $("#licence_no");
    $frName = $("#frName");
    $frId = $("#frId");
    $jsName = $("#jsName");
    $jsNum = $("#jsNum");
    $fistSubmit = $("#frist-submit");
    $lodinPassword = $("#lodin-password");
    $passwordAgain = $("#password-again");
    $nextSubmit = $("#next-submit");
    window.errorObject = new errorDialog();
    logoutObject = new Logout();

}